from PIL import Image
import sys 
import os
import subprocess
###这是一个先把图片大小改成1000以下，然后两张图片进行拼接的程序###
###输入是文件夹路径，然后遍历图片，两两匹配###
def get_imlist(path, fmt_input):
    return [(os.path.join(path, f), os.stat(os.path.join(path, f)).st_mtime) for f in os.listdir(path) if f.endswith(fmt_input)]

def resizePhoto(infile,outfile):
    photo = Image.open(infile)
    (x,y) = photo.size
    if x>1000 or y>1000:
        if x>y:
            x_s = 1000
            y_s = 1000*y//x
        else:
            y_s = 1000
            x_s = 1000*y//x
        #print x_s,y_s
        out = photo.resize((x_s,y_s),Image.ANTIALIAS)
        out.save(infile)


def switch(argument):
    switcher = {
        "jpg": "JPG",
        "png": "PNG",
        "pgm": "PGM",
    }
    return switcher.get(argument, "nothing")
def sift_match(infile_1,infile_2,outfile_3):
    print("suc")
    pFeatures = subprocess.Popen([os.path.join("./match.exe"), infile_1, infile_2, outfile_3])
    pFeatures.wait()

def main(path,fmt):
    im_list = get_imlist(path, fmt) + get_imlist(path, switch(fmt))
    #print (im_list)
    if len(im_list) % 2 == 0:
        for index in range(len(im_list) // 2):
            print(index)
            resizePhoto(im_list[index * 2],index * 2)
            resizePhoto(im_list[index * 2 + 1],index * 2 + 1)
            file1 = os.path.split(im_list[index * 2])[1].split(".")[0]
            file2 = os.path.split(im_list[index * 2 + 1])[1].split(".")[0]
            filename = file1 + "_" + file2 + "." + fmt
            sift_match(im_list[index * 2], im_list[index * 2 + 1], os.path.join(path,filename))
        
            #os.rename(im_list[index * 2],os.path.join(path, str(index * 2) + "." + fmt))
            #os.rename(im_list[index * 2 + 1],os.path.join(path, str(index * 2 + 1) + "." + fmt))
    else:
        for index in range((len(im_list) - 1) // 2):
            print(index)
            resizePhoto(im_list[index * 2], index * 2)
            resizePhoto(im_list[index * 2 + 1], index * 2 + 1)
            file1 = os.path.split(im_list[index * 2])[1].split(".")[0]
            file2 = os.path.split(im_list[index * 2 + 1])[1].split(".")[0]
            filename = file1 + "_" + file2 + "." + fmt
            sift_match(im_list[index * 2], im_list[index * 2 + 1], os.path.join(path, filename))

            #os.rename(im_list[index * 2], os.path.join(path , str(index * 2) + "." + fmt))
            #os.rename(im_list[index * 2 + 1],os.path.join(path, str(index * 2 + 1) + "." + fmt))


def sift_resize(index,im_list):
    file1 = os.path.join(path, str(index * 2) + "." + fmt)
    file2 = os.path.join(path, str(index * 2 + 1) + "." + fmt)
    resizePhoto(file1, file1)
    resizePhoto(file2, file2)
    image_file1 = os.path.split(file1)[1].split(".")[0]
    image_file2 = os.path.split(file2)[1].split(".")[0]
    filename = image_file1 + "_" +image_file2 + "." + fmt
    sift_match(file1, file2, os.path.join(path, filename))
    print("#####" + image_file1 + "++++++++" + image_file2 + "==========" + filename)

def main1(path, fmt):
    ###解决文件乱序问题，既用名称排序拼接###
    im_list = get_imlist(path, fmt) + get_imlist(path, switch(fmt))
    #print (im_list)
    if len(im_list) % 2 == 0:
        for index in range(len(im_list) // 2):
            sift_resize(index,im_list)
    else:
        for index in range((len(im_list) - 1) // 2):
            sift_resize(index,im_list)
path = sys.argv[1]
fmt = sys.argv[2]

if __name__ == '__main__':
    #main(path,fmt)
    #print(get_imlist(path,fmt))
    main1(path,fmt)